﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Web.Model;
using Portal.Cms.Model;
using Portal.Web.Utility;
using Portal.Cms.Bussiness;
using Portal.Core;
using Portal.Cms.Utility;

namespace Portal.Cms.Render
{
    public class HomeRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string RootIcon = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                DataTypeCls[] DataType = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDataType(SiteId);
                string Html =
                   "<div class=FormFunction>\r\n" +
                   "    <h3>CÁC CHỨC NĂNG CỦA HỆ THỐNG QUẢN LÝ WEBSITE</h3>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div style=\"margin-top:2px;text-align:left;padding:8px;font-size:22px;\">\r\n" +
                   "            <div style=\"font-size:16px;margin-top:4px;padding:4px;color:Gray;font-style:italic\">Chào mừng bạn đã đến với hệ thống quản lý website</div>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                   "            <ul>\r\n";
                for (int iIndex = 0; iIndex < DataType.Length; iIndex++)
                {
                    string Url = "RootGroupData.aspx?DataType=" + DataType[iIndex].DataTypeId;
                    Html +=
                       "                <li class=li"+DataType[iIndex].DataTypeId+" onclick=\"window.open('"+Url+"','_self');\">\r\n" +
                       "                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/cms/" + DataType[iIndex].DataTypeId + ".png\"></div>\r\n" +
                       "                    <div>" + DataType[iIndex].DataTypeName + "</div>\r\n" +
                       "                </li>\r\n";
                }

                //bool AllowPublish = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcPublish, grou, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                //if (AllowPublish)
                //{
                    //Html +=
                    //   "                <li class=liPublish onclick=\"window.open('RootApprovedData.aspx','_self');\">\r\n" +
                    //   "                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/cms/Publish.png\"></div>\r\n" +
                    //   "                    <div>Duyệt phát hành</div>\r\n" +
                    //   "                </li>\r\n";
                //}

                //doan nay load me plug-in
                //bool AllowStatitic = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, StatiticsPermission.StaticId, StatiticsPermission.funcAccess, "Statitics", WebSessionUtility.GetCurrentLoginUser().UserId, false);
                
                //if (AllowStatitic)
                //{
                //    Html +=
                //       "                <li class=liStatitics onclick=\"window.open('DataStatitics.aspx','_self');\">\r\n" +
                //       "                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/cms/Statitics.png\"></div>\r\n" +
                //       "                    <div>Thống kê báo cáo</div>\r\n" +
                //       "                </li>\r\n";
                //}
                Html+=
                   //"                <li class=liSchedule onclick=\"window.open('Schedule.aspx','_self');\">\r\n" +
                   //"                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/Cms/Schedule.png\"></div>\r\n" +
                   //"                    <div>Lịch công tác</div>\r\n" +
                   //"                </li>\r\n" +
                   //"                <li class=liComment onclick=\"window.open('Comment.aspx','_self');\">\r\n" +
                   //"                    <div><img style=\"height:60px;\" src=\"" + RootIcon + "/cms/Comment.png\"></div>\r\n" +
                   //"                    <div>Ý kiến đóng góp</div>\r\n" +
                   //"                </li>\r\n"+


                    //"                <li class=liSystemLog onclick=\"window.open('SystemLog.aspx','_self');\">\r\n" +
                    //"                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/SystemLog.png\"></div>\r\n" +
                    //"                    <div>Xem log truy cập</div>\r\n" +
                    //"                </li>\r\n"+

                       "                <li class=liBack onclick=\"window.open('../../folder.aspx','_self');\">\r\n" +
                       "                    <div><img style=\"height:60px;\" src=\""+RootIcon+"/Back.png\"></div>\r\n" +
                       "                    <div>Quay lại</div>\r\n" +
                       "                </li>\r\n"+
                       "            </ul>\r\n" +
                       "        </div>\r\n" +
                       "    </div>\r\n" +
                       "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            
            return OAjaxOut;
        }





        public static AjaxOut DrawDataRootMenu(string SiteId,string DataType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTypeCls ODataType = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateDataTypeModel(SiteId, DataType);
                //string Root = "RootDataInput.aspx";
                //if (ODataType.DataTypeId.Equals("document"))
                //{
                //    Root = "RootDataInputDocument.aspx";
                //}

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string RootUrl = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                GroupDataCls[] GroupDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataForInput(SiteId, DataType);
                string Html =
                   "<div class=FormFunction>\r\n" +
                   "    <h3>CÁC CHỨC NĂNG CỦA HỆ THỐNG QUẢN LÝ WEBSITE</h3>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div style=\"margin-top:2px;text-align:left;padding:8px;font-size:22px;\">\r\n" +
                   "            <div style=\"font-size:16px;margin-top:4px;padding:4px;color:Gray;font-style:italic\">Chào mừng bạn đã đến với hệ thống quản lý website</div>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                   "            <ul>\r\n";
                if (WebSessionUtility.GetCurrentLoginUser().IsAdmin==1 || WebSessionUtility.GetCurrentLoginUser().IsSystemAdmin)
                {
                    Html +=
                       "                <li class=liNews onclick=\"window.open('GroupData.aspx?datatype=" + DataType + "','_self');\">\r\n" +
                       "                    <div><img style=\"height:60px;\" src=\""+RootUrl+"/cms/manage.png\"></div>\r\n" +
                       "                    <div>Quản lý " + ODataType.DataTypeName + "</div>\r\n" +
                       "                </li>\r\n";
                }

                int Total = 0;
                for (int iIndex = 0; iIndex < GroupDatas.Length; iIndex++)
                {
                    string gid = GroupDatas[iIndex].GroupDataId;
                    bool AllowAccessed=SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                    if (AllowAccessed)
                    {
                        Total++;
                        string Url = "InputData.aspx?gid=" + gid;
                        if (DataType.Equals("document"))
                        {
                            Url = "DocumentInputData.aspx?gid=" + gid;
                        }
                        if (DataType.Equals("vote"))
                        {
                            Url = "SurveyInputData.aspx?gid=" + gid;
                        }
                        
                        Html +=
                           "                <li class=liDocumentType onclick=\"window.open('" + Url + "','_self');\">\r\n" +
                           "                    <div><img style=\"height:60px;\" src=\""+RootUrl+"/cms/" + GroupDatas[iIndex].frkDataTypeId + ".png\"></div>\r\n" +
                           "                    <div>" + GroupDatas[iIndex].GroupDataName + "</div>\r\n" +
                           "                </li>\r\n";
                    }
                }

                if (Total == 0)
                {
                }
                Html+=

                   //"                <li class=liInputNews onclick=\"window.open('"+Root+"?DataType=" + DataType + "','_self');\">\r\n" +
                   //"                    <div><img style=\"height:60px;\" src=\"Resources/Images/news.png\"></div>\r\n" +
                   //"                    <div>Nhập " + ODataType.DataTypeName + "</div>\r\n" +
                   //"                </li>\r\n" +
                       "                <li class=liBack onclick=\"window.open('home.aspx','_self');\">\r\n" +
                       "                    <div><img style=\"height:60px;\" src=\""+ButtonUrl+"/Back.png\"></div>\r\n" +
                       "                    <div>Quay lại</div>\r\n" +
                       "                </li>\r\n" +
                       "            </ul>\r\n" +
                       "        </div>\r\n" +
                       "    </div>\r\n" +
                       "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        public static AjaxOut DrawRootDataInput(string SiteId,string DataType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Root = "RootGroupData.aspx";
                
                if (DataType.Equals("document"))
                {
                    Root = "RootGroupDataDocument.aspx";
                
                }
                GroupDataCls[] GroupDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataForInput(SiteId, DataType);
                string Html =
                   "<div class=FormFunction>\r\n" +
                   "    <h3>Nhập tin tức</h3>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div style=\"margin-top:2px;text-align:left;padding:8px;font-size:22px;\">\r\n" +
                   "            <div style=\"font-size:16px;margin-top:4px;padding:4px;color:Gray;font-style:italic\">Chào mừng bạn đã đến với chức năng quản lý văn bản tài liệu</div>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                   "            <ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDatas.Length; iIndex++)
                {
                    string gid = GroupDatas[iIndex].GroupDataId;
                    string Url = "InputData.aspx?gid=" + gid;
                    if (DataType.Equals("document"))
                    {
                        Url = "Documents.aspx?gid="+gid;

                    }
                    
                    Html +=
                       "                <li class=liDocumentType onclick=\"window.open('"+Url+"','_self');\">\r\n" +
                       "                    <div><img style=\"height:60px;\" src=\"Resources/Images/news.png\"></div>\r\n" +
                       "                    <div>" + GroupDatas[iIndex].GroupDataName + "</div>\r\n" +
                       "                </li>\r\n";
                }
                Html +=
                   "                <li class=liBack onclick=\"window.open('"+Root+"?DataType="+DataType+"','_self');\">\r\n" +
                   "                    <div><img style=\"height:60px;\" src=\"Resources/Images/back.png\"></div>\r\n" +
                   "                    <div>Quay lại</div>\r\n" +
                   "                </li>\r\n" +
                   "            </ul>\r\n" +
                   "        </div>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        public static AjaxOut DrawRootApprovedData(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
//                string Root = "RootApprovedData.aspx";
                GroupDataCls[] GroupDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataForInput(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrlRoot = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                string Html =
                   "<div class=FormFunction>\r\n" +
                   "    <h3>Duyệt chuyên mục</h3>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div style=\"margin-top:2px;text-align:left;padding:8px;font-size:22px;\">\r\n" +
                   "            <div style=\"font-size:16px;margin-top:4px;padding:4px;color:Gray;font-style:italic\">Vui lòng chọn vào nhóm dữ liệu để duyệt</div>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                   "            <ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDatas.Length; iIndex++)
                {
                    string gid = GroupDatas[iIndex].GroupDataId;
                    bool AllowPublish = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcPublish, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                    if (AllowPublish)
                    {
                        string Url = "ApprovedData.aspx?gid=" + gid;

                        Html +=
                           "                <li class=liDocumentType onclick=\"window.open('" + Url + "','_self');\">\r\n" +
                           "                    <div><img style=\"height:60px;\" src=\""+ButtonUrlRoot+"/news.png\"></div>\r\n" +
                           "                    <div>" + GroupDatas[iIndex].GroupDataName + "</div>\r\n" +
                           "                </li>\r\n";
                    }
                }
                Html +=
                   "                <li class=liBack onclick=\"window.open('Home.aspx','_self');\">\r\n" +
                   "                    <div><img style=\"height:60px;\" src=\""+ButtonUrlRoot+"/back.png\"></div>\r\n" +
                   "                    <div>Quay lại</div>\r\n" +
                   "                </li>\r\n" +
                   "            </ul>\r\n" +
                   "        </div>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
